programming:cpp:Cpp operator overloadingWhat links here?
http://www.cplusplus.com/doc/tutorial/classes2.html

type operator sign (parameters) { /*...*/ }

Here you have an example that overloads the addition operator (+). We are going to create a class to store bidimensional vectors and then we are going to add two of them: a(3,1) and b(1,2). The addition of two bidimensional vectors is an operation as simple as adding the two x coordinates to obtain the resulting x coordinate and adding the two y coordinates to obtain the resulting y. In this case the result will be (3+1,1+2) = (4,3).

// vectors: overloading operators example
  1. include using namespace std;


class my_vector { public: int x,y; my_vector () {} ; my_vector (int,int); my_vector operator + (my_vector); };

my_vector::my_vector (int a, int b) { x = a; y = b; }

my_vector my_vector::operator+ (my_vector param) { my_vector temp; temp.x = x + param.x; temp.y = y + param.y; return (temp); }

int main () { my_vector a (3,1); my_vector b (1,2); my_vector c; c = a + b; cout MALFORMED HTML TAG: << c.x << "," << c.y; return 0; }
programming:cpp:Cpp operator overloading
filename:programming:cpp:Cpp operator overloading
filename:programming%3Acpp%3ACpp%20operator%20overloading
last edit:September 09 2009 20:02:54 (5351 days ago)
ct = 1714941554.000000 = May 05 2024 16:39:14
ft = 1252540974.000000 = September 09 2009 20:02:54
dt = 462400580.000000